clear
clear mata
clear matrix

set mem 500m
set maxvar 32000
set scheme s1color
set more off, perm

macro def path_data "/disk/data3/cens1940/byvar"                               /* This is the folder where Census data is stored */

* Daniel Feenberg: where do you want to save temporary files? Set the path below and save the code in XXX/code and the data in XXX/data
cd XXX
mkdir /tmp                                                                    /* creates a temporary folder to store temp files */
macro def path XXX/tmp

capture log close
log using "$path/log/NBER_Patriotism.log", append

*       Patriotism!

*       FIRST VERSION  Summer 2021
*       THIS VERSION   Spring 2022
*       LAST RUN       Spring 2022

*       LAST REVISOR		BC

*       Log of revisions:

*       This prepares a database which matches the machine-encoded full count of 
*       the 1940 US population and the WW2 military records (NARA Army Serial 
*       Number data). Note:
*       - Matching is performed with Abramitsky, Boustan and Eriksson procedure
*       - IPUMS data is proprietary and interested researchers must seek authorization
*         to access them by writing to ipumsres@mnu.edu

**************************************************************************************
****                          PLAN OF THE PROCEDURE                               ****
****   1. Download relevant packages (should be there, but just in case)          ****
****   2. Prepare the US Census records for the match                             ****
****      a. Prepare data from the 4 regions                                      ****
****         i. Loop through each of the 4 regions                                ****
****        ii. Keep only the men                                                 ****
****       iii. Drop duplicates (bichos raros)                                    ****
****        iv. Keep only men born between 1898 & 1931                            ****
****         v. Add all other variables (place of birth & names)                  ****
****        vi. Rename variables and save                                         ****
****      b. Append data from 4 regions                                           ****
****      c. Clean the variable of interest                                       ****
****         i. Place of birth                                                    ****
****        ii. Year of birth                                                     ****
****       iii. Keep only what's needed, order & label                            ****
****      d. Save a separate file for each state-yob combination                  ****
****         i. US States                                                         ****
****        ii. Foreign countries                                                 ****
****   3. Find all matches                                                        ****
****      a. Loop over all places of birth                                        ****
****      b. Get state code                                                       ****
****         i. US States                                                         ****
****        ii. Foreign countries                                                 ****
****      c. Standardize data                                                     ****
****         i. Military recruitment data (ASN)                                   ****
****        ii. Census data (C40)                                                 ****
****      d. Match names                                                          ****
****      e. Apply restrictions for more conservative matching                    ****
****      f. Append all files and save                                            ****
****   4. Add all other Census variables to the database                          ****
****      a. Prepare the ids of men correctly matched                             ****
****         i. Prepare the ids from each  state                                  ****
****        ii. Append all the states                                             ****
****       iii. Create a database with identifiers of only households             ****
****      b. Household head-level variables                                       ****
****         i. Get household head variables for the people matched               ****
****        ii. Append the files from the 4 regions and rename variables          ****
****       iii. Order, label and save                                             ****
****            1. Rename all the variables                                       ****
****            2. Clean variables                                                ****
****            3. Label variables                                                ****
****            4. Save                                                           ****
****      c. Household-level variables                                            ****
****         i. Get the 3 variables of interest for the households matched        ****
****        ii. Append the databases from the 4 regions toghether                 ****
****       iii. Clean the variables of interst                                    ****
****        iv. Collapse at the household level and create the indicators         ****
****         v. Label, order and save                                             ****
****      d. Individual-level variables                                           ****
****         i. Get all the variables for the people matched                      ****
****            1. 5% sample extra questions                                      ****
****            2. Full sample variables                                          ****
****        ii. Append the files from the 4 regions and rename variables          ****
****       iii. Order, label and save                                             ****
****            1. Rename all the variables                                       ****
****            2. Clean variables                                                ****
****            3. Label variables                                                ****
****            4. Drop junk                                                      ****
****            5. Order                                                          ****
****            6. Merge with the household-level information                     ****
****               A. Household head-level variables                              ****
****               B. Household level variables                                   ****
****            7. Merge to match IDs                                             ****
****            8. Save                                                           ****
****   5. Create CEM matched samples                                              ****
****      a. Prepare data                                                         ****
****      b.  Loop over every county in the database                              ****
****      c. Create CEM indicators                                                ****
****         i. Treatment: works for WPA                                          ****
****        ii. Treatment: owns a house                                           ****
****       iii. Treatment: owns a farm                                            ****
****      d. Save CEM indicators for this county and drop it                      ****
****      e. Append CEM indicators for every county                               ****
****      f. Merge CEM indicator to the matched database                          ****
****   6. Match data to county-level information                                  ****
****   7. Create variables of interest                                            ****
****   8. Label and save                                                          ****
****   9. Erase junk created                                                      ****
****______________________________________________________________________________****
****                                                                              ****
**************************************************************************************

**************************************************************************************
****   1. Download relevant packages (should be there, but just in case)          ****
**************************************************************************************
cap ssc install nysiis      /* this package is required at some point */
cap ssc install jarowinkler /* this package is required at some point */
cap ssc install parallel    /* this package is required at some point */

**************************************************************************************
****   2. Prepare the US Census records for the match                             ****
****      a. Prepare data from the 4 regions                                      ****
****         i. Loop through each of the 4 regions                                ****
**************************************************************************************
foreach region in MIDWEST NORTHEAST SOUTH WEST {
	use "$path_data/`region'32", replace

**************************************************************************************
****        ii. Keep only the men                                                 ****
**************************************************************************************
	keep if self_empty_info_gender == "Male"
	
**************************************************************************************
****       iii. Drop duplicates (bichos raros)                                    ****
**************************************************************************************
	duplicates tag hhid_numeric hhorder , gen(DUP)
	drop if DUP > 0        /* duplicates are "bichos raros": there was 1 in NORTHEAST */
	drop    DUP

**************************************************************************************
****        iv. Keep only men born between 1898 & 1931                            ****
**************************************************************************************
	merge 1:m hhid_numeric hhorder using "$path_data/`region'34", keep(mat) nogen
	destring self_residence_info_age, gen(C40AGE) force     /* This is creating missing for all people with age = x/12, where x in [1,12]. These are kids younger than 1 year */
	keep if C40AGE >= 8 & C40AGE <= 42 & C40AGE != .        /* Official census day is the 1st April 1940. I take cohorts with YOB within 1898 & 1931 (+/-2 years from 99% of my boys) */

	drop self_residence_info_age self_empty_info_gender 

	duplicates tag hhid_numeric hhorder , gen(DUP)
	drop if DUP > 0        /* duplicates are "bichos raros": there was 1 in NORTHEAST. NOTE that this was a different one from the one above! */
	drop    DUP
	
**************************************************************************************
****         v. Add all other variables (place of birth & names)                  ****
**************************************************************************************
	foreach var in 26 27 39 {
		merge 1:m hhid_numeric hhorder using "$path_data/`region'`var'", keep(mat) nogen
		
		duplicates tag hhid_numeric hhorder , gen(DUP)
		drop if DUP > 0        /* duplicates are "bichos raros": this is to be 100% sure that there are no problems. */
		drop    DUP
	} /* foreach var */
		
**************************************************************************************
****        vi. Rename variables and save                                         ****
**************************************************************************************
	rename self_empty_name_given   C40NAME
	rename self_empty_name_surname C40SURNAME
	rename self_birth_place_empty  C40BPLRAW

	tostring hhid_numeric, replace
	gen C40REG = "`region'"
	
	compress
	
	save "$path/tmp/C40-`region'", replace
} /* foreach region */

**************************************************************************************
****      b. Append data from 4 regions                                           ****
**************************************************************************************
clear
for REGION in any MIDWEST NORTHEAST SOUTH WEST : append using "$path/tmp/C40-REGION"

**************************************************************************************
****      c. Clean the variable of interest                                       ****
****         i. Place of birth                                                    ****
**************************************************************************************
include "$path/code/NBER-C40BPL.do"

**************************************************************************************
****        ii. Year of birth                                                     ****
**************************************************************************************
gen C40YOB      = 1940 - C40AGE

**************************************************************************************
****       iii. Keep only what's needed, order & label                            ****
**************************************************************************************
keep  C40REG hhid_numeric hhorder C40NAME C40SURNAME C40BPL C40YOB
order C40REG hhid_numeric hhorder C40NAME C40SURNAME C40BPL C40YOB

compress
lab var C40REG       "Macro-region in US"
lab var hhid_numeric "Household ID within macro-region"
lab var hhorder      "Person ID within its household"
lab var C40NAME      "First name"
lab var C40SURNAME   "Family name"
lab var C40BPL       "Place of birth"
lab var C40YOB       "Year of birth"

**************************************************************************************
****      d. Save a separate file for each state-yob combination                  ****
**************************************************************************************
foreach state in AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY CD IT GE EN MX PO PH IE RU PR CN AT CZ GR HU SE PN NO YU FR RO CU DK PT HO TR CH ES LT FI BE AB BG JP EG {	

**************************************************************************************
****         i. US States                                                         ****
**************************************************************************************
	if "`state'" == "AL"  local bpl = 41
	if "`state'" == "AK"  local bpl = 81
	if "`state'" == "AZ"  local bpl = 61
	if "`state'" == "AR"  local bpl = 42
	if "`state'" == "CA"  local bpl = 71
	if "`state'" == "CO"  local bpl = 62
	if "`state'" == "CT"  local bpl =  1
	if "`state'" == "DE"  local bpl = 11
	if "`state'" == "DC"  local bpl = 98
	if "`state'" == "FL"  local bpl = 43
	if "`state'" == "GA"  local bpl = 44
	if "`state'" == "HI"  local bpl = 82
	if "`state'" == "ID"  local bpl = 63
	if "`state'" == "IL"  local bpl = 21
	if "`state'" == "IN"  local bpl = 22
	if "`state'" == "IA"  local bpl = 31
	if "`state'" == "KS"  local bpl = 32
	if "`state'" == "KY"  local bpl = 51
	if "`state'" == "LA"  local bpl = 45
	if "`state'" == "ME"  local bpl =  2
	if "`state'" == "MD"  local bpl = 52
	if "`state'" == "MA"  local bpl =  3
	if "`state'" == "MI"  local bpl = 23
	if "`state'" == "MN"  local bpl = 33
	if "`state'" == "MS"  local bpl = 46
	if "`state'" == "MO"  local bpl = 34
	if "`state'" == "MT"  local bpl = 64
	if "`state'" == "NE"  local bpl = 35
	if "`state'" == "NV"  local bpl = 65
	if "`state'" == "NH"  local bpl =  4
	if "`state'" == "NJ"  local bpl = 12
	if "`state'" == "NM"  local bpl = 66
	if "`state'" == "NY"  local bpl = 13
	if "`state'" == "NC"  local bpl = 47
	if "`state'" == "ND"  local bpl = 36
	if "`state'" == "OH"  local bpl = 24
	if "`state'" == "OK"  local bpl = 53
	if "`state'" == "OR"  local bpl = 72
	if "`state'" == "PA"  local bpl = 14
	if "`state'" == "RI"  local bpl =  5
	if "`state'" == "SC"  local bpl = 48
	if "`state'" == "SD"  local bpl = 37
	if "`state'" == "TN"  local bpl = 54
	if "`state'" == "TX"  local bpl = 49
	if "`state'" == "UT"  local bpl = 67
	if "`state'" == "VT"  local bpl =  6
	if "`state'" == "VA"  local bpl = 40
	if "`state'" == "WA"  local bpl = 73
	if "`state'" == "WV"  local bpl = 56
	if "`state'" == "WI"  local bpl = 25
	if "`state'" == "WY"  local bpl = 68
	
**************************************************************************************
****        ii. Foreign countries                                                 ****
**************************************************************************************
	if "`state'" == "CD"  local bpl = 150
	if "`state'" == "IT"  local bpl = 434
	if "`state'" == "GE"  local bpl = 453
	if "`state'" == "EN"  local bpl = 410
	if "`state'" == "MX"  local bpl = 200
	if "`state'" == "PO"  local bpl = 455
	if "`state'" == "PH"  local bpl = 515
	if "`state'" == "IE"  local bpl = 414
	if "`state'" == "RU"  local bpl = 465
	if "`state'" == "PR"  local bpl = 110
	if "`state'" == "CN"  local bpl = 500
	if "`state'" == "AT"  local bpl = 450
	if "`state'" == "CZ"  local bpl = 452
	if "`state'" == "GR"  local bpl = 433
	if "`state'" == "HU"  local bpl = 454
	if "`state'" == "SE"  local bpl = 405
	if "`state'" == "PN"  local bpl = 130
	if "`state'" == "NO"  local bpl = 404
	if "`state'" == "YU"  local bpl = 457
	if "`state'" == "FR"  local bpl = 421
	if "`state'" == "RO"  local bpl = 456
	if "`state'" == "CU"  local bpl = 250
	if "`state'" == "DK"  local bpl = 400
	if "`state'" == "PT"  local bpl = 436
	if "`state'" == "HO"  local bpl = 425
	if "`state'" == "TR"  local bpl = 542
	if "`state'" == "CH"  local bpl = 426
	if "`state'" == "ES"  local bpl = 438
	if "`state'" == "LT"  local bpl = 462
	if "`state'" == "FI"  local bpl = 401
	if "`state'" == "BE"  local bpl = 420
	if "`state'" == "AB"  local bpl = 430
	if "`state'" == "BG"  local bpl = 451
	if "`state'" == "JP"  local bpl = 501
	if "`state'" == "EG"  local bpl = 600
		
	preserve
		keep if C40BPL == `bpl'
		drop    C40BPL
		compress
		save "$path/tmp/C40-`state'", replace
	restore
	drop if C40BPL == `bpl'
} /* foreach state */

**************************************************************************************
****   3. Find all matches                                                        ****
****      a. Loop over all places of birth                                        ****
**************************************************************************************
foreach state in AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY CD IT GE EN MX PO PH IE RU PR CN AT CZ GR HU SE PN NO YU FR RO CU DK PT HO TR ES LT FI BE AB BG JP EG {	

**************************************************************************************
****      b. Get state code                                                       ****
****         i. US States                                                         ****
**************************************************************************************
	if "`state'" == "AL"  local bpl = 41
	if "`state'" == "AK"  local bpl = 81
	if "`state'" == "AZ"  local bpl = 61
	if "`state'" == "AR"  local bpl = 42
	if "`state'" == "CA"  local bpl = 71
	if "`state'" == "CO"  local bpl = 62
	if "`state'" == "CT"  local bpl =  1
	if "`state'" == "DE"  local bpl = 11
	if "`state'" == "DC"  local bpl = 98
	if "`state'" == "FL"  local bpl = 43
	if "`state'" == "GA"  local bpl = 44
	if "`state'" == "HI"  local bpl = 82
	if "`state'" == "ID"  local bpl = 63
	if "`state'" == "IL"  local bpl = 21
	if "`state'" == "IN"  local bpl = 22
	if "`state'" == "IA"  local bpl = 31
	if "`state'" == "KS"  local bpl = 32
	if "`state'" == "KY"  local bpl = 51
	if "`state'" == "LA"  local bpl = 45
	if "`state'" == "ME"  local bpl =  2
	if "`state'" == "MD"  local bpl = 52
	if "`state'" == "MA"  local bpl =  3
	if "`state'" == "MI"  local bpl = 23
	if "`state'" == "MN"  local bpl = 33
	if "`state'" == "MS"  local bpl = 46
	if "`state'" == "MO"  local bpl = 34
	if "`state'" == "MT"  local bpl = 64
	if "`state'" == "NE"  local bpl = 35
	if "`state'" == "NV"  local bpl = 65
	if "`state'" == "NH"  local bpl =  4
	if "`state'" == "NJ"  local bpl = 12
	if "`state'" == "NM"  local bpl = 66
	if "`state'" == "NY"  local bpl = 13
	if "`state'" == "NC"  local bpl = 47
	if "`state'" == "ND"  local bpl = 36
	if "`state'" == "OH"  local bpl = 24
	if "`state'" == "OK"  local bpl = 53
	if "`state'" == "OR"  local bpl = 72
	if "`state'" == "PA"  local bpl = 14
	if "`state'" == "RI"  local bpl =  5
	if "`state'" == "SC"  local bpl = 48
	if "`state'" == "SD"  local bpl = 37
	if "`state'" == "TN"  local bpl = 54
	if "`state'" == "TX"  local bpl = 49
	if "`state'" == "UT"  local bpl = 67
	if "`state'" == "VT"  local bpl =  6
	if "`state'" == "VA"  local bpl = 40
	if "`state'" == "WA"  local bpl = 73
	if "`state'" == "WV"  local bpl = 56
	if "`state'" == "WI"  local bpl = 25
	if "`state'" == "WY"  local bpl = 68
	
**************************************************************************************
****        ii. Foreign countries                                                 ****
**************************************************************************************
	if "`state'" == "CD"  local bpl = 150
	if "`state'" == "IT"  local bpl = 434
	if "`state'" == "GE"  local bpl = 453
	if "`state'" == "EN"  local bpl = 410
	if "`state'" == "MX"  local bpl = 200
	if "`state'" == "PO"  local bpl = 455
	if "`state'" == "PH"  local bpl = 515
	if "`state'" == "IE"  local bpl = 414
	if "`state'" == "RU"  local bpl = 465
	if "`state'" == "PR"  local bpl = 110
	if "`state'" == "CN"  local bpl = 500
	if "`state'" == "AT"  local bpl = 450
	if "`state'" == "CZ"  local bpl = 452
	if "`state'" == "GR"  local bpl = 433
	if "`state'" == "HU"  local bpl = 454
	if "`state'" == "SE"  local bpl = 405
	if "`state'" == "PN"  local bpl = 130
	if "`state'" == "NO"  local bpl = 404
	if "`state'" == "YU"  local bpl = 457
	if "`state'" == "FR"  local bpl = 421
	if "`state'" == "RO"  local bpl = 456
	if "`state'" == "CU"  local bpl = 250
	if "`state'" == "DK"  local bpl = 400
	if "`state'" == "PT"  local bpl = 436
	if "`state'" == "HO"  local bpl = 425
	if "`state'" == "TR"  local bpl = 542
	if "`state'" == "CH"  local bpl = 426
	if "`state'" == "ES"  local bpl = 438
	if "`state'" == "LT"  local bpl = 462
	if "`state'" == "FI"  local bpl = 401
	if "`state'" == "BE"  local bpl = 420
	if "`state'" == "AB"  local bpl = 430
	if "`state'" == "BG"  local bpl = 451
	if "`state'" == "JP"  local bpl = 501
	if "`state'" == "EG"  local bpl = 600

**************************************************************************************
****      c. Standardize data                                                     ****
****         i. Military recruitment data (ASN)                                   ****
**************************************************************************************
	use $path/ASN-`state', replace         /* These files contain the NARA Army Serial Numbers records divided by State */
	
	keep if pob == `state'
	
	rename name    f_name 						// call first name "f_name"
	rename surname l_name 						// call last name "l_name"

	tostring serialu, gen(id) format(%30.0f)
	
	gen sex = 1                                 /* These are all men */
	abeclean f_name l_name, sex(sex) nicknames initial(middleinitial)
	drop sex

	save $path/ASN-`state'_r2l, replace    /* Military records file ready to link */
	
**************************************************************************************
****        ii. Census data (C40)                                                 ****
**************************************************************************************
	use $path/tmp/C40-`state', replace         /* These files contain the USA full count records divided by State */

	rename C40NAME    f_name 				    // call first name "f_name"
	rename C40SURNAME l_name 					// call last name "l_name"

	tostring household_identifier, replace
	tostring individual_identifier     , replace
	gen id = substr(C40REG,1,1) + "_" + household_identifier + "_" + individual_identifier
	                                            /* This creates a unique identifier for every person in the Census */

	gen sex = 1                                 /* These are all men */
	abeclean f_name l_name, sex(sex) nicknames initial(middleinitial)
	drop sex
	ren C40YOB yob
	
	save $path/tmp/C40-`state'_r2l, replace    /* US Census records file ready to link */
	
**************************************************************************************
****      d. Match names                                                          ****
**************************************************************************************
	abematch f_name_nysiis l_name_nysiis,  file_A($path/tmp/ASN-`state'_r2l) file_B($path/tmp/C40-`state'_r2l) timevar(yob) timediff(0) save($path/tmp/C40ASN-`state')  replace id_A(id) id_B(id) unique_m(2) unique_f(2)

**************************************************************************************
****      e. Apply restrictions for more conservative matching                    ****
**************************************************************************************
	use $path/tmp/C40ASN-`state', replace

	keep if unique_file2 == 1  // keep only names unique within +- 2 years in own data sets
	keep if unique_match2 == 1 // drop people that have another potential match within +-2 years of birth

	save $path/tmp/C40ASN-`state'_5yr_band, replace
	                                         /* Final, matched database */
} /* foreach state */

**************************************************************************************
****      f. Append all files and save                                            ****
**************************************************************************************
clear
foreach state in AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY {	
	append using $path/tmp/C40ASN-`state'_5yr_band
} /* foreach state */

foreach state in CD IT GE EN MX PO PH IE RU PR CN AT CZ GR HU SE PN NO YU FR RO CU DK PT HO TR ES LT FI BE AB BG JP EG {	
	append using $path/tmp/C40ASN-`state'_5yr_band
} /* foreach state */

save $path/tmp/C40ASN_5yr_band, replace

**************************************************************************************
****   4. Add all other Census variables to the database                          ****
****      a. Prepare the ids of men correctly matched                             ****
****         i. Prepare the ids from each  state                                  ****
**************************************************************************************

foreach state in AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY CD IT GE EN MX PO PH IE RU PR CN AT CZ GR HU SE PN NO YU FR RO CU DK PT HO TR ES LT FI BE AB BG JP EG {	
	use $path/cens1940.work/C40-`state', replace

	tostring hhid_numeric, replace
	tostring hhorder     , replace
	gen id_B = substr(C40REG,1,1) + "_" + hhid_numeric + "_" + hhorder
	
	keep id_B C40REG hhid_numeric hhorder
	
	merge 1:1 id_B using $path/tmp/C40ASN_5yr_band, nogen keep(mat)
	
	save $path/tmp/C40ASN-`state'-ids, replace
} /* foreach state */

**************************************************************************************
****        ii. Append all the states                                             ****
**************************************************************************************
clear
foreach state in AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY CD IT GE EN MX PO PH IE RU PR CN AT CZ GR HU SE PN NO YU FR RO CU DK PT HO TR ES LT FI BE AB BG JP EG {	
	append using $path/tmp/C40ASN-`state'-ids
} /* foreach state */

save $path/tmp/C40ASN_5yr_band_ids, replace

**************************************************************************************
****       iii. Create a database with identifiers of only households             ****
**************************************************************************************
use $path/tmp/C40ASN_5yr_band_ids, replace

keep C40 hhid_numeric
duplicates drop

save $path/tmp/C40ASN_5yr_band_hhids, replace

**************************************************************************************
****      b. Household head-level variables                                       ****
****         i. Get household head variables for the people matched               ****
**************************************************************************************
foreach region in MIDWEST NORTHEAST SOUTH WEST {
	forvalues var_num = 22(1)24 {
            if `var_num' == 22 local var = "general_homeownrent"
	    if `var_num' == 23 local var = "general_homevalue"
	    if `var_num' == 24 local var = "general_farm"

	    capture use $path_data/`region'`var_num', replace
	    if _rc == 601 {    /* The files for the WEST have not been imported in Stata yet */
	        import delimited $path_data/`region'`var_num'.txt, delimiter("|") clear
	        tostring  hhid_numeric, replace
	    }  /* if _rc = 601 */

	    duplicates tag hhid_numeric hhorder, gen(DUP)
	    drop if DUP > 0                                                                         /* duplicates are "bichos raros": there was 2 in NORTHEAST (a total of 5 observations), of which 1 male, actually */
	    drop    DUP

	            keep if hhorder == 1                                                            /* These variables are asked only to the head of the household */
                    drop    hhorder
            capture keep if `var' != ""
	    capture keep if `var' != .
		
	    gen C40REG = "`region'"
	    tostring hhid_numeric, replace
	    merge 1:m C40REG hhid_numeric using $path/tmp/C40ASN_5yr_band_hhids, nogen keep(mat) keepus(C40REG hhid_numeric)

	    save $path/tmp/`region'-`var_num', replace
	} /* forvalues var_num */
	
	use $path/tmp/`region'-22, replace
	forvalues var_num = 23(1)24 {
		merge 1:1 hhid_numeric using $path/tmp/`region'-`var_num', nogen
	}  /* forvalues var_num */
	
	save $path/tmp/`region'-headvariables, replace
} /* foreach region */

**************************************************************************************
****        ii. Append the files from the 4 regions and rename variables          ****
**************************************************************************************
clear
foreach region in MIDWEST NORTHEAST SOUTH WEST {
	append using $path/tmp/`region'-headvariables
} /* foreach region */

**************************************************************************************
****       iii. Order, label and save                                             ****
****            1. Rename all the variables                                       ****
**************************************************************************************
rename general_homeownrent              C40HOMEOWNRAW
rename general_homevalue                C40HOMEVALUERAW
rename general_farm                     C40FARM

**************************************************************************************
****            2. Clean variables                                                ****
**************************************************************************************
replace C40FARM = cond(C40FARM == "Yes" | C40FARM == "YES" | C40FARM == "yes","1",cond(C40FARM == "No" | C40FARM == "NO" | C40FARM == "no" | C40FARM == "nO","0",C40FARM))
gen C40HOMEOWN = cond(C40HOMEOWNRAW == "Owned" ,1,  ///
                 cond(C40HOMEOWNRAW == "Rented",0,.))

gen     C40HOMEVALUE =          C40HOMEVALUERAW
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,",","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE," ","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"?","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"-","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"per month","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"Month","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"Mouth","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"mouth","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"per mo","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"permo","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"mth","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"pm","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"MO","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"Mo","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"MT","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"mo","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"M","",.)
replace C40HOMEVALUE = subinstr(C40HOMEVALUE,"m","",.)

destring C40FARM     , replace
destring C40HOMEVALUE, replace force

**************************************************************************************
****            3. Label variables                                                ****
**************************************************************************************
lab var C40FARM              "=1 if lives on farm"
lab var C40HOMEOWN           "=1 home is owned"
lab var C40HOMEVALUE         "Home value"
lab var C40HOMEOWNRAW        "=1 home is owned (raw)"
lab var C40HOMEVALUERAW      "Home value (raw)"

lab def yesno 1 "Yes" 0 "No"
lab val C40FARM yesno
lab def home_own_lbl 1 "Owned" 0 "Rented"
lab val C40HOMEOWN home_own_lbl

**************************************************************************************
****            4. Save                                                           ****
**************************************************************************************
save $path/tmp/C40ASN-heads, replace

**************************************************************************************
****      c. Household-level variables                                            ****
****         i. Get the 3 variables of interest for the households matched        ****
**************************************************************************************
foreach region in MIDWEST NORTHEAST SOUTH WEST {
	foreach var_num in 49 53 54 65 {
	    if `var_num' == 49 local var = "general_employmentcode"
	    if `var_num' == 53 local var = "general_workerclass"
	    if `var_num' == 54 local var = "general_occupationcode"
            if `var_num' == 65 local var = "general_ssn"
		
		capture use $path_data/`region'`var_num', replace
		if _rc == 601 {    /* The files for the WEST have not been imported in Stata yet */
			import delimited $path_data/`region'`var_num'.txt, delimiter("|") clear
			tostring  hhid_numeric, replace
		}  /* if _rc = 601 */
	
		duplicates tag hhid_numeric hhorder, gen(DUP)
		drop if DUP > 0                                                                         /* duplicates are "bichos raros": there were 2 in NORTHEAST (a total of 5 observations), of which 1 male, actually */
		drop    DUP

	    capture keep if `var' != ""
	    capture keep if `var' != .
		
		gen C40REG = "`region'"
		tostring hhid_numeric, replace
		merge m:1 C40REG hhid_numeric using $path/tmp/C40ASN_5yr_band_hhids, nogen keep(mat) keepus(C40REG hhid_numeric)

		save $path/tmp/`region'HH-`var_num', replace
	} /* forvalues var_num */
	
	use $path/tmp/`region'HH-49, replace
	foreach var_num in 53 54 65 {
		merge 1:1 hhid_numeric hhorder using $path/tmp/`region'HH-`var_num', nogen
	}  /* forvalues var_num */
	save $path/tmp/`region'HH, replace
} /* foreach region */

**************************************************************************************
****        ii. Append the databases from the 4 regions toghether                 ****
**************************************************************************************
clear
foreach region in MIDWEST NORTHEAST SOUTH WEST {
	append using $path/tmp/`region'HH
} /* foreach region */

**************************************************************************************
****       iii. Clean the variables of interst                                    ****
**************************************************************************************
rename general_employmentcode           C40EMPLCODERAW
rename general_workerclass              C40ROLE
rename general_occupationcode           C40CODEOCC

destring C40EMPLCODERAW, gen(C40EMPLCODE) force

gen C40HHWPA  = inlist(C40EMPLCODE,2)
gen C40HHGW   = inlist(C40ROLE    ,"All Government Worker","CW","Cw","G","GW PW","GWPW","Gw Pw","Pw Gw","Wage or salary worker in Government work")

replace general_ssn =       lower(general_ssn)
gen     C40HHSSN    = 0 if        general_ssn != ""
replace C40HHSSN    = 1 if strpos(general_ssn,"yes") > 0

split       C40CODEOCC, gen(occ)

gen C40HHPOST = occ2 == "95" | occ2 == "095"
gen C40HHNDEF = occ2 == "96" | occ2 == "096"
gen C40HHFGVT = occ2 == "97" | occ2 == "097"
gen C40HHLGVT = occ2 == "98" | occ2 == "098"

gen C40HHSIZE = 1

**************************************************************************************
****        iv. Collapse at the household level and create the indicators         ****
**************************************************************************************
collapse (sum) C40HHSIZE C40HHWPA C40HHGW C40HHPOST C40HHNDEF C40HHFGVT C40HHLGVT C40HHSSN (count) N_C40HHSSN = C40HHSSN, by(C40REG hhid_numeric)

replace   C40HHSSN = . if N_C40HHSSN == 0
drop    N_C40HHSSN
for VAR in var C40HHWPA C40HHGW C40HHPOST C40HHNDEF C40HHFGVT C40HHLGVT C40HHSSN : gen VAR1 = (VAR > 0) if VAR != .

**************************************************************************************
****         v. Label, order and save                                             ****
**************************************************************************************
lab var C40HHSIZE   "Number of household members"
lab var C40HHWPA    "Number of household members with relief job (WPA, CCC,…)"
lab var C40HHGW     "Number of household members who are public employees"
lab var C40HHPOST   "Number of household members who work for the federal post office"
lab var C40HHNDEF   "Number of household members who work for national defence"
lab var C40HHFGVT   "Number of household members who work for the Federal government"
lab var C40HHLGVT   "Number of household members who work for the State or local government"
lab var C40HHSSN    "Number of household members with SSN (from 5% sample: missing if no hh member answered)"
lab var C40HHWPA1   "At least 1 household member has relief job (WPA, CCC,…)"
lab var C40HHGW1    "At least 1 household member is public employees"
lab var C40HHPOST1  "At least 1 household member works for the federal post office"
lab var C40HHNDEF1  "At least 1 household member works for national defence"
lab var C40HHFGVT1  "At least 1 household member works the Federal government"
lab var C40HHLGVT1  "At least 1 household member works the State or local government"
lab var C40HHSSN1   "At least 1 household member with a social security number"

order C40REG hhid_numeric *1
save $path/tmp/C40ASN-households, replace

**************************************************************************************
****      d. Individual-level variables                                           ****
****         i. Get all the variables for the people matched                      ****
****            1. 5% sample extra questions                                      ****
**************************************************************************************
foreach region in MIDWEST NORTHEAST SOUTH WEST {
	forvalues var_num = 59(1)68 {
		if `var_num' == 59 local var = "father_birth_place_empty"
		if `var_num' == 60 local var = "mother_birth_place_empty"
		if `var_num' == 61 local var = "general_nativelanguage"
		if `var_num' == 62 local var = "general_veteran"
		if `var_num' == 63 local var = "general_veteranfather"
		if `var_num' == 64 local var = "general_militaryservice"
		if `var_num' == 65 local var = "general_ssn"
		if `var_num' == 66 local var = "general_usualoccupation"
		if `var_num' == 67 local var = "general_usualindustry"
		if `var_num' == 68 local var = "general_usualclassofworker"
		
		capture use $path_data/`region'`var_num', replace
		if _rc == 601 {    /* The files for the WEST have not been imported in Stata yet */
			import delimited $path_data/`region'`var_num'.txt, delimiter("|") clear
			tostring  hhid_numeric, replace
		}  /* if _rc = 601 */
 		
		keep if `var' != ""
		duplicates tag hhid_numeric hhorder, gen(DUP)
		drop if DUP > 0                                                                         /* duplicates are "bichos raros": there was 2 in NORTHEAST (a total of 5 observations), of which 1 male, actually */
		drop    DUP
		
		gen C40REG = "`region'"
		tostring hhid_numeric, replace
		tostring hhorder     , replace
		merge 1:1 C40REG hhid_numeric hhorder using $path/tmp/C40ASN_5yr_band_ids, nogen keep(mat) keepus(C40REG hhid_numeric hhorder)
	
		save $path/tmp/`region'-5sample-`var_num', replace		
	} /* forvalues var_num */
	
	use $path/tmp/`region'-5sample-59, replace
	
	forvalues var_num = 60(1)68 {
		merge 1:1 hhid_numeric hhorder using $path/tmp/`region'-5sample-`var_num', nogen
	}  /* forvalues var_num */
	
	gen C40SAMPLE5 = 1
	save $path/tmp/`region'-5sample, replace
} /* foreach region */ 

**************************************************************************************
****            2. Full sample variables                                          ****
**************************************************************************************
foreach region in MIDWEST NORTHEAST SOUTH WEST {
	forvalues var_num = 3(1)58 {
        if `var_num' == 22 | `var_num' == 23 | `var_num' == 24 continue
        if `var_num' == 3  local var = "self_residence_place_state"
        if `var_num' == 4  local var = "self_residence_place_county"
	    if `var_num' == 5  local var = "general_naratownship"
        if `var_num' == 6  local var = "general_township_orig"
	    if `var_num' == 7  local var = "general_township"
	    if `var_num' == 8  local var = "general_incorporatedplace_orig"
	    if `var_num' == 9  local var = "general_incorporatedplace"
        if `var_num' == 10 local var = "general_ward"
	    if `var_num' == 11 local var = "general_unincorporatedplace_orig"
        if `var_num' == 12 local var = "general_unincorporatedplace"
	    if `var_num' == 13 local var = "general_nara_enumeration_distric"
        if `var_num' == 14 local var = "indexed_enumeration_district"
	    if `var_num' == 15 local var = "general_institution_orig"
	    if `var_num' == 16 local var = "general_institution"
	    if `var_num' == 17 local var = "general_sheetnumber"
	    if `var_num' == 18 local var = "general_line_number"
        if `var_num' == 19 local var = "indexed_street"
	    if `var_num' == 20 local var = "general_house_number"
	    if `var_num' == 21 local var = "general_household_visited"
        if `var_num' == 25 local var = "self_empty_name_prefix"
	    if `var_num' == 26 local var = "self_empty_name_given"
        if `var_num' == 27 local var = "self_empty_name_surname"
	    if `var_num' == 28 local var = "self_empty_name_suffix"
        if `var_num' == 29 local var = "general_respondent"
	    if `var_num' == 30 local var = "general_tempabsent"
	    if `var_num' == 31 local var = "self_empty_info_relationtohead"
        if `var_num' == 32 local var = "self_empty_info_gender"
	    if `var_num' == 33 local var = "self_empty_info_race"
	    if `var_num' == 34 local var = "self_residence_info_age"
	    if `var_num' == 35 local var = "self_empty_info_maritalstatus"
	    if `var_num' == 36 local var = "general_attendedschool"
	    if `var_num' == 37 local var = "general_gradecompleted"
	    if `var_num' == 38 local var = "general_self_birth_place_empty_o"
	    if `var_num' == 39 local var = "self_birth_place_empty"
	    if `var_num' == 40 local var = "general_citizenship_orig"
	    if `var_num' == 41 local var = "general_citizenship"
	    if `var_num' == 42 local var = "self_residence_place_city_multip"
	    if `var_num' == 43 local var = "general_self_residence_place_cit"
	    if `var_num' == 44 local var = "self_residence_place_county_mult"
	    if `var_num' == 45 local var = "general_self_residence_place_cou"
	    if `var_num' == 46 local var = "self_residence_place_state_multi"
	    if `var_num' == 47 local var = "general_self_residence_place_sta"
	    if `var_num' == 48 local var = "general_residencefarm1935"
	    if `var_num' == 49 local var = "general_employmentcode"
	    if `var_num' == 50 local var = "general_hoursworked"
	    if `var_num' == 51 local var = "general_durationunemployment"
	    if `var_num' == 52 local var = "general_occupation"
	    if `var_num' == 53 local var = "general_workerclass"
	    if `var_num' == 54 local var = "general_occupationcode"
	    if `var_num' == 55 local var = "general_weeksworked"
	    if `var_num' == 56 local var = "general_income"
	    if `var_num' == 57 local var = "general_incomeothersources"
	    if `var_num' == 58 local var = "general_namesupp"
		
		if "`region'" == "WEST" & `var_num' == 46 {    /* this asshole gives a "segmentation fault" on NBER linux */
			import delimited $path_data/`region'`var_num'.txt, delimiter("|") clear
			tostring  hhid_numeric, replace
		}  /* if WEST, var_num = 46 */

		else {
			capture use $path_data/`region'`var_num', replace
			if _rc == 601 {    /* The files for the WEST have not been imported in Stata yet */
				import delimited $path_data/`region'`var_num'.txt, delimiter("|") clear
				tostring  hhid_numeric, replace
			}  /* if _rc = 601 */
 		} /* else */
	
		duplicates tag hhid_numeric hhorder, gen(DUP)
		drop if DUP > 0                                                                         /* duplicates are "bichos raros": there was 2 in NORTHEAST (a total of 5 observations), of which 1 male, actually */
		drop    DUP

	    capture keep if `var' != ""
	    capture keep if `var' != .
		
		gen C40REG = "`region'"
		tostring hhid_numeric, replace
		tostring hhorder     , replace
		merge 1:1 C40REG hhid_numeric hhorder using $path/tmp/C40ASN_5yr_band_ids, nogen keep(mat) keepus(C40REG hhid_numeric hhorder)

		save $path/tmp/`region'-`var_num', replace
	} /* forvalues var_num */
	
	use $path/tmp/`region'-3, replace
	forvalues var_num = 4(1)58 {
                if `var_num' == 22 | `var_num' == 23 | `var_num' == 24 continue
		merge 1:1 hhid_numeric hhorder using $path/tmp/`region'-`var_num', nogen
	}  /* forvalues var_num */
	
	merge 1:1 hhid_numeric hhorder using $path/tmp/`region'-5sample, nogen
	
	save $path/tmp/`region'-allvariables, replace
} /* foreach region */
	
**************************************************************************************
****        ii. Append the files from the 4 regions and rename variables          ****
**************************************************************************************
use $path/tmp/SOUTH-allvariables, replace
tostring general_namesupp, replace
save $path/tmp/SOUTH-allvariables, replace 

use $path/tmp/WEST-allvariables, replace
tostring general_namesupp, replace
save $path/tmp/WEST-allvariables, replace 

clear
foreach region in MIDWEST NORTHEAST SOUTH WEST {
	append using $path/tmp/`region'-allvariables
} /* foreach region */

**************************************************************************************
****       iii. Order, label and save                                             ****
****            1. Rename all the variables                                       ****
**************************************************************************************
rename self_empty_name_prefix           C40PREX
rename self_empty_name_given            C40NAME
rename self_empty_name_surname          C40SURNAME
rename self_empty_name_suffix           C40SUFFX

rename self_empty_info_race             C40RACERAW
rename self_residence_info_age          C40AGE

rename self_residence_place_state       C40STATERAW
rename self_residence_place_county      C40COUNTY
rename general_township_orig            C40TOWNSHIPRAW
rename general_township                 C40TOWNSHIP
rename general_incorporatedplace        C40INCORPORATED
rename general_unincorporatedplace      C40UNINCORPORATED
rename general_incorporatedplace_orig   C40INCORPORATEDRAW
rename general_unincorporatedplace_orig C40UNINCORPORATEDRAW
rename general_ward                     C40WARD
rename indexed_enumeration_district     C40ENUMDIST  
rename general_institution              C40INST
rename general_institution_orig         C40INSTRAW
rename general_sheetnumber              C40SHEET
rename general_line_number              C40LINE
rename indexed_street                   C40STREET
rename general_house_number             C40HOUSENR
rename general_household_visited        C40HHNR
rename general_respondent               C40RESPONDENT
rename general_tempabsent               C40ABSENT
rename self_empty_info_relationtohead   C40HHROLE
rename self_empty_info_gender           C40SEX
rename self_empty_info_maritalstatus    C40MARITAL
rename general_attendedschool           C40SCHOOLRAW
rename general_gradecompleted           C40SCHOOLGRADERAW
rename general_citizenship              C40NATUR
rename general_residencefarm1935        C40FARM35
rename general_employmentcode           C40EMPLCODERAW
rename general_hoursworked              C40HOURSRAW
rename general_durationunemployment     C40WEEKSUNEMPRAW
rename general_occupation               C40OCC
rename general_occupationcode           C40CODEOCC
rename general_workerclass              C40ROLE
rename general_weeksworked              C40WEEKSWORKRAW
rename general_veteran                  C40VETRAW
rename general_militaryservice          C40WWIRAW
rename general_income                   C40INCOMERAW
rename general_incomeothersources       C40INCOMEOTHERRAW
rename father_birth_place_empty         C40FATHBPLRAW
rename mother_birth_place_empty         C40MOTHBPLRAW
rename general_nativelanguage           C40LANG
rename general_veteranfather            C40FATHVETRAW
rename general_ssn                      C40SSNRAW
rename general_usualoccupation          C40USUALOCCRAW
rename general_usualindustry            C40USUALINDRAW
rename general_usualclassofworker       C40USUALROLERAW

rename general_self_residence_place_cit C40CITY35RAW
rename general_self_residence_place_cou C40COUNTY35RAW
rename general_self_residence_place_sta C40STATE35RAW
rename self_residence_place_city_multip C40SELFREPORTCITY35RAW
rename self_residence_place_county_mult C40SELFREPORTCOUNTY35RAW
rename self_residence_place_state_multi C40SELFREPORTSTATE35RAW

rename self_birth_place_empty           C40BPLRAW

**************************************************************************************
****            2. Clean variables                                                ****
**************************************************************************************
recode C40SAMPLE5 (.=0)

include "$path/code/NBER-C40BPL.do"
include "$path/code/NBER-C40STATE.do"
include "$path/code/NBER-C40RACE.do"

gen C40SCHOOL      = C40SCHOOLRAW
gen C40INCOMEOTHER = C40INCOMEOTHERRAW

for X in var C40FARM35                                        : replace X = cond(X == "Yes" | X == "YES" | X == "yes","1",cond(X == "No" | X == "NO" | X == "no" | X == "nO","0",X))
for X in var C40RESPONDENT C40ABSENT C40SCHOOL C40INCOMEOTHER : replace X = cond(X == "Yes","1","0")

replace C40SEX     = cond(C40SEX     == "Male" ,"1",cond(C40SEX == "Female","0",C40SEX))

replace C40MARITAL = "1" if inlist(C40MARITAL,"Casado (Married)","Married","Contrato Consensual","Consensual Contract","Married[Single]","Married[Widowed (W) (Widowed)]")
replace C40MARITAL = "3" if inlist(C40MARITAL,"Divorced","Separated")
replace C40MARITAL = "5" if inlist(C40MARITAL,"Viudo (Widower)","Widowed","Widow")
replace C40MARITAL = "6" if inlist(C40MARITAL,"Single","Soltero (Single)","Bachelor")

gen     C40NATUR0 = "1" if inlist(C40NATUR,"American Citizen","American Citizen Born Abroad","Citizen","Us")
replace C40NATUR0 = "1" if inlist(C40NATUR,"American Citizen born Abroad","Cit","Naturalized","Y","Yes","yes")
replace C40NATUR  = "1" if inlist(C40NATUR,"American Citizen","American Citizen Born Abroad","Citizen","Us")
replace C40NATUR  = "1" if inlist(C40NATUR,"American Citizen born Abroad","Cit","Naturalized","Y","Yes","yes")
replace C40NATUR  = "1" if inlist(C40NATUR,"Having First Papers","Having first Papers")

replace C40NATUR0 = "0" if C40NATUR != "" & C40NATUR0 != "1"
replace C40NATUR  = "0" if C40NATUR != "" & C40NATUR  != "1"

gen C40VET = cond(C40SAMPLE5 == 1 &  C40VETRAW == "Yes",1, ///
             cond(C40SAMPLE5 == 1 &  C40VETRAW != "Yes",0,.))
gen C40WWI = cond(C40SAMPLE5 == 1 & (C40WWIRAW == "World War" | C40WWIRAW == "War"),1,  ///
             cond(C40SAMPLE5 == 1 &  C40WWIRAW != "World War" & C40WWIRAW != "War" ,0,.))
gen C40SSN = cond(C40SAMPLE5 == 1 &  C40SSNRAW == "Yes",1, ///
             cond(C40SAMPLE5 == 1 &  C40SSNRAW == "No" ,0,.))
			 
gen     C40INCOMETYPE = 0
replace C40INCOMETYPE = 1 if C40INCOMERAW == ""
replace C40INCOMETYPE = 2 if (       C40INCOMERAW      == "N" |        C40INCOMERAW        == "No" |        C40INCOMERAW         == "None")
replace C40INCOMETYPE = 3 if (strpos(C40INCOMERAW,"+")  >  0  | strpos(C40INCOMERAW,"Est")  >  0   | strpos(C40INCOMERAW,"Less")  >  0    | strpos(C40INCOMERAW,"Over"  )  >  0 | strpos(C40INCOMERAW,"Under")  >  0)
replace C40INCOMETYPE = 4 if (strpos(C40INCOMERAW,"C")  >  0  | strpos(C40INCOMERAW,"OA" )  >  0   | strpos(C40INCOMERAW,"Oa"  )  >  0    | strpos(C40INCOMERAW,"Refuse")  >  0 | strpos(C40INCOMERAW,"Send" )  >  0 | strpos(C40INCOMERAW,"Information")  >  0)

gen     C40INCOME = C40INCOMERAW
replace C40INCOME = subinstr(C40INCOME,","    ,"" ,.)
replace C40INCOME = subinstr(C40INCOME,"+"    ,"" ,.)
replace C40INCOME = subinstr(C40INCOME,"Est" ,"" ,.)
replace C40INCOME = subinstr(C40INCOME,"Less" ,"" ,.)
replace C40INCOME = subinstr(C40INCOME,"Under","" ,.)
replace C40INCOME = subinstr(C40INCOME,"Over" ,"" ,.)
replace C40INCOME = subinstr(C40INCOME,"None","0",.)
replace C40INCOME = subinstr(C40INCOME,"No"  ,"0",.)
replace C40INCOME = subinstr(C40INCOME,"N"   ,"0",.)
			 
destring C40HOURSRAW     , gen(C40HOURS)      force
destring C40WEEKSUNEMPRAW, gen(C40WEEKSUNEMP) force
destring C40WEEKSWORKRAW , gen(C40WEEKSWORK ) force
destring C40INCOMEOTHER  , replace            force
destring C40AGE C40FARM35 C40RESPONDENT C40ABSENT C40MARITAL C40NATUR C40NATUR0 C40SEX, replace
destring C40SCHOOL , replace force
destring C40INCOME , replace force

gen C40HASINCOME = (C40INCOME > 0 | C40INCOMERAW == "Y" | C40INCOMERAW == "Yes")
gen C40MARRIED   = C40MARITAL < 6 if C40MARITAL != . 

rename C40SCHOOL C40SCHOOL1940

 gen     C40SCHOOL_0 = inlist(C40SCHOOLGRADERAW,"None","No","0","Ill","Ilit","Il") if C40SCHOOLGRADERAW != ""
 
 gen     C40SCHOOL_E = strpos(C40SCHOOLGRADERAW,"Elementary school") > 0           if C40SCHOOLGRADERAW != ""
 replace C40SCHOOL_E = 1                                                           if C40SCHOOLGRADERAW != "" & C40SCHOOL_E == 0 & inlist(C40SCHOOLGRADERAW,"El","Ele","E10")

 gen     C40SCHOOL_H = strpos(C40SCHOOLGRADERAW,"High School"      ) > 0           if C40SCHOOLGRADERAW != ""
 replace C40SCHOOL_H = 1                                                           if C40SCHOOLGRADERAW != "" & C40SCHOOL_H == 0 & inlist(C40SCHOOLGRADERAW,"H8","H5","H7","H","H6","Hs","H0","8 4h")
 replace C40SCHOOL_H = 1                                                           if C40SCHOOLGRADERAW != "" & C40SCHOOL_H == 0 & inlist(C40SCHOOLGRADERAW,"5H","7H","7h","3/H1","H1 9","H13","3-h")
 replace C40SCHOOL_H = 1                                                           if C40SCHOOLGRADERAW != "" & C40SCHOOL_H == 0 & inlist(C40SCHOOLGRADERAW,"H23","H??","HC","HS","High2","3 Yrs H S")
 
 gen     C40SCHOOL_C = strpos(C40SCHOOLGRADERAW,"College"          ) > 0           if C40SCHOOLGRADERAW != ""
 replace C40SCHOOL_C = 1                                                           if C40SCHOOLGRADERAW != "" & C40SCHOOL_C == 0 & inlist(C40SCHOOLGRADERAW,"C6","C8","C7","4C","C9","4c","C10","8C")
 replace C40SCHOOL_C = 1                                                           if C40SCHOOLGRADERAW != "" & C40SCHOOL_C == 0 & inlist(C40SCHOOLGRADERAW,"Cs","C11","10C","C12","7C","8c","C0","10c")
 replace C40SCHOOL_C = 1                                                           if C40SCHOOLGRADERAW != "" & C40SCHOOL_C == 0 & inlist(C40SCHOOLGRADERAW,"11C","2 Yes Col","7c","9C","C14","C15")
 replace C40SCHOOL_C = 1                                                           if C40SCHOOLGRADERAW != "" & C40SCHOOL_C == 0 & inlist(C40SCHOOLGRADERAW,"C2 H4","C3 H","C4 H4","C4c","C8 1/2","Cc","Col Gr")
 
 gen C40SCHOOL = cond(C40SCHOOL_0 == 1,0,  ///
                 cond(C40SCHOOL_E == 1,1,  ///
                 cond(C40SCHOOL_H == 1,2,  ///
                 cond(C40SCHOOL_C == 1,3,.)))) if C40SCHOOLGRADERAW != ""

 gen C40HIGHSCHOOL = (C40SCHOOL > 1) if C40SCHOOL != .

 replace C40WEEKSWORK = 0 if C40WEEKSWORK == . & inlist(C40WEEKSWORKRAW,"N","No","None")
 replace C40WEEKSWORK = 6 if C40WEEKSWORK == . &        C40WEEKSWORKRAW == "6 Wks" 
 
 gen C40WORK = (C40WEEKSWORK > 0) if C40WEEKSWORK != . & C40WEEKSWORK < 53
 gen C40QUARTERWORK = cond(C40WEEKSWORK ==  0                    ,0, ///
                      cond(C40WEEKSWORK >   0 & C40WEEKSWORK < 14,1, ///
                      cond(C40WEEKSWORK >= 14 & C40WEEKSWORK < 27,2, ///
                      cond(C40WEEKSWORK >= 27 & C40WEEKSWORK < 39,3, ///
                      cond(C40WEEKSWORK >= 39 & C40WEEKSWORK < 53,4,.)))))


gen C40HEAD      = (C40HHROLE == "Head") if C40HHROLE != ""
gen C40IMMIGRANT = (C40BPL > 90) if C40BPL != .

gen     C40PW    = inlist(C40ROLE,"P/w","Pw Yes","Wage or salary worker","Wage or salary worker in private work")
gen     C40GW    = inlist(C40ROLE,"All Government Worker","CW","Cw","G","GW PW","GWPW","Gw Pw","Pw Gw","Wage or salary worker in Government work")
gen     C40E     = inlist(C40ROLE,"E Pw","Employer","PW E")
gen     C40OA    = inlist(C40ROLE,"AO","Ao","E OA","E Oa","Gw Oa","Np Oa","O/a","OA PW","OC")
replace C40OA    = inlist(C40ROLE,"Oa E","Oa Gw","Oa Oa","Oa Pw","Ooa","PW OA","Pw Oa","Working on own account") if C40OA == 0
gen     C40NP    = inlist(C40ROLE,"Pw Np","Unpaid family worker")
gen     C40F     = inlist(C40ROLE,"F","Farm","Fw")

destring C40EMPLCODERAW, gen(C40EMPLCODE) force
gen C40WPA  = inlist(C40EMPLCODE,2)

split       C40CODEOCC, gen(occ)

gen C40POST = occ2 == "95" | occ2 == "095"
gen C40NDEF = occ2 == "96" | occ2 == "096"
gen C40FGVT = occ2 == "97" | occ2 == "097"
gen C40LGVT = occ2 == "98" | occ2 == "098"

drop occ* C40EMPLCODE

**************************************************************************************
****            3. Label variables                                                ****
**************************************************************************************				 
lab var C40REG               "Macro-region"
lab var C40STATE             "State of residence"
lab var C40COUNTY            "County of residence"
lab var C40TOWNSHIP          "Township of residence"
lab var C40TOWNSHIPRAW       "Township of residence (raw)"
lab var C40INCORPORATED      "Incorporated place"
lab var C40UNINCORPORATED    "Unincorporated place"
lab var C40ENUMDIST          "Enumeration district"
lab var C40SHEET             "Enumeration sheet number"
lab var C40LINE              "Enumeration line number"
lab var C40STREET            "Street address"
lab var C40WARD              "Ward of city"
lab var C40HOUSENR           "House number"
lab var C40PREX              "Prefix"
lab var C40NAME              "First name"
lab var C40SURNAME           "Family name"
lab var C40SUFFX             "Suffix"
lab var C40HHROLE            "Relation to the head of the house"
lab var C40FARM35            "=1 if lives on farm in 1935"
lab var C40SEX               "=1 if man"
lab var C40RACE              "Race"
lab var C40SCHOOL            "=1 if attended school anytime since March 1 1940"
lab var C40NATUR             "Naturalization"
lab var C40NATUR0            "Naturalization (strict: 'first papers' is not counted)"
lab var C40HOURS             "Hours worked in week of March 24-30 1940"
lab var C40WEEKSWORK         "Number of weeks worked in 1939"
lab var C40WEEKSUNEMP        "Duration of unemployment until March 30 1940"
lab var C40OCC               "Occupation"
lab var C40CODEOCC           "Occupation code"
lab var C40ROLE              "Class of worker"
lab var C40VET               "=1 if veteran"
lab var C40WWI               "=1 if took part to World War I"
lab var C40INCOME            "Income in 1939"
lab var C40HASINCOME         "=1 if has positive income"
lab var C40INCOMETYPE        "Type of problem in income data (if any)"
lab var C40INCOMEOTHER       "=1 if he has income from other sources"
lab var C40SAMPLE5           "=1 if part of the 5% sample asked questions on veteran status"
lab var C40LANG              "Mothertongue"
lab var C40SSN               "=1 if he has a Social Security Number"
lab var C40INST              "Institution"
lab var C40HHNR              "Number of household in order of visitation"	
lab var C40RESPONDENT        "=1 if the person responded"
lab var C40ABSENT            "=1 if the person was temporarily absent"
lab var C40MARITAL           "Marital status"
lab var C40MARRIED           "=1 if ever married"
lab var C40SCHOOL_0              "Does he have no schooling at all?"
lab var C40SCHOOL_E              "Highest school grade attained was elementary (any grade)?"
lab var C40SCHOOL_H              "Highest school grade attained was high school (any grade)?"
lab var C40SCHOOL_C              "Highest school grade attained was high school (any grade)?"
lab var C40SCHOOL                "Highest school grade attained (4 categories)"
lab var C40HIGHSCHOOL            "Highest school grade attained was in high school or in college?"
lab var C40WORK                  "Worked at least 1 week in 1939?"
lab var C40QUARTERWORK           "Number of quarters worked duringm 1939"
lab var C40AGE                   "Age in years as of 1940"
lab var C40HEAD                  "Is the guy is head of his household?"
lab var C40IMMIGRANT             "Born outside of the US?"
lab var C40PW                    "=1 if class of worker is private worker"
lab var C40GW                    "=1 if class of worker is public worker"
lab var C40E                     "=1 if class of worker is entrepreneur"
lab var C40OA                    "=1 if class of worker is own account"
lab var C40NP                    "=1 if class of worker is non-paid family worker"
lab var C40F                     "=1 if class of worker is farmer"
lab var C40WPA                   "Has relief job (WPA, CCC,…)"
lab var C40POST                  "Works for the federal post office"
lab var C40NDEF                  "Works for national defence"
lab var C40FGVT                  "Works the Federal government"
lab var C40LGVT                  "Works the State or local government"

lab var C40RACERAW           "Race (raw)"
lab var C40INSTRAW           "Institution (raw)"
lab var C40INCORPORATEDRAW   "Incorporated place (raw)"
lab var C40UNINCORPORATEDRAW "Unincorporated place (raw)"
lab var C40SCHOOLRAW         "=1 if attended school anytime since March 1 1940 (raw)"
lab var C40SCHOOLGRADERAW    "Highest grade of school completed (raw)"
lab var C40EMPLCODERAW       "Employment code (raw)"
lab var C40HOURSRAW          "Hours worked in week of March 24-30 1940 (raw)"
lab var C40WEEKSUNEMPRAW     "Duration of unemployment until March 30 1940 (raw)"
lab var C40WEEKSWORKRAW      "Number of weeks worked in 1939 (raw)"
lab var C40INCOMERAW         "Income in 1939 (string: raw)"
lab var C40INCOMEOTHERRAW    "=1 if he has income from other sources (raw)"
lab var C40VETRAW            "Veteran status (raw)"
lab var C40WWIRAW            "Type of military service (raw)"
lab var C40FATHBPLRAW        "Father's birthplace (raw)"
lab var C40MOTHBPLRAW        "Mother's birthplace (raw)"
lab var C40FATHVETRAW        "Veteran status of the father (raw)"
lab var C40SSNRAW            "Indicator for Social Security Number (raw)"
lab var C40USUALOCCRAW       "Usual occupation (raw)"
lab var C40USUALINDRAW       "Usual industry of occupation (raw)"
lab var C40USUALROLERAW      "Usual class of worker (raw)"
lab var C40CITY35RAW             "City of residence in 1935 (raw)"
lab var C40COUNTY35RAW           "County of residence in 1935 (raw)"
lab var C40STATE35RAW            "State of residence in 1935 (raw)"
lab var C40SELFREPORTCITY35RAW   "City of residence in 1935 (self reported, raw)"
lab var C40SELFREPORTCOUNTY35RAW "County of residence in 1935 (self reported, raw)"
lab var C40SELFREPORTSTATE35RAW  "State of residence in 1935 (self reported, raw)"
lab def yesno 1 "Yes" 0 "No"
lab val C40FARM35 C40SCHOOL C40NATUR C40NATUR0 C40MARRIED yesno

lab def        sex_lbl 1 "Male" 0 "Female"
lab val C40SEX sex_lbl

label define marst_lbl 1 `"Married, spouse present"'
label define marst_lbl 2 `"Married, spouse absent"', add
label define marst_lbl 3 `"Separated"', add
label define marst_lbl 4 `"Divorced"', add
label define marst_lbl 5 `"Widowed"', add
label define marst_lbl 6 `"Never married/single"', add

label values C40MARITAL marst_lbl

lab def C40INCOMETYPE_lbl 0 "No problem" 1 "Missing record" 2 "0 income (No/N/None)" 3 "Approximate (e.g. +/Under/Over...)" 4 "Other strings"
lab val C40INCOMETYPE C40INCOMETYPE_lbl

**************************************************************************************
****            4. Drop junk                                                      ****
**************************************************************************************
drop if C40SEX == 0       /* This should not drop anybody, but just in case... */

drop C40STATERAW
drop general_citizenship_orig                                               /* I kept general_citizenship */
drop general_naratownship general_nara_enumeration_distric general_namesupp /* These are always missing */
drop general_self_birth_place_empty_o 
                                                                            /* I already had these */																			
**************************************************************************************
****            5. Order                                                          ****
**************************************************************************************
order C40REG hhid_numeric hhorder C40STATE C40COUNTY C40TOWNSHIP C40INCORPORATED          ///
      C40UNINCORPORATED C40ENUMDIST C40SHEET C40LINE C40HHNR C40RESPONDENT                ///
	  C40PREX C40NAME C40SURNAME C40SUFFX C40RACE C40AGE                              ///
	  C40ABSENT C40HEAD C40HHROLE   C40BPL C40SEX C40VET C40WWI                       ///	  
	  C40LANG C40SCHOOL* C40HIGHSCHOOL C40MARITAL C40MARRIED C40IMMIGRANT C40NATUR*   ///
	  C40WORK C40FARM* C40INCOME C40HASINCOME C40INCOMETYPE C40INCOMEOTHER C40HOURS   ///
	  C40WEEKS* C40QUARTERWORK C40OCC C40CODEOCC C40ROLE C40PW-C40F C40WPA-C40LGVT    ///
	  C40SSN C40INST C40WARD C40SAMPLE5 C40STREET C40HOUSENR C40USUAL* *RAW

**************************************************************************************
****            6. Merge with the household-level information                     ****
****               A. Household head-level variables                              ****
**************************************************************************************
merge m:1 C40REG hhid_numeric         using $path/tmp/C40ASN-heads     , nogen keep(mas mat)
order C40FARM C40HOMEOWN C40HOMEVALUE, after(C40ABSENT)

**************************************************************************************
****               B. Household level variables                                   ****
**************************************************************************************
merge m:1 C40REG hhid_numeric         using $path/tmp/C40ASN-households, nogen keep(mas mat)


**************************************************************************************
****            7. Merge to match IDs                                             ****
**************************************************************************************
merge 1:1 C40REG hhid_numeric hhorder using $path/tmp/C40ASN_5yr_band_ids      , nogen keepus(id_B)

**************************************************************************************
****            8. Save                                                           ****
**************************************************************************************
compress
save $path/data/C40ASN, replace

**************************************************************************************
****   5. Create CEM matched samples                                              ****
****      a. Prepare data                                                         ****
**************************************************************************************
use $path/data/C40ASN, replace

keeporder C40REG household_identifier individual_identifier stateicpsr countyicpsr40 C40AGE C40RACE C40IMMIGRANT C40INCOME C40FARM C40HOMEOWN C40WPA C40SCHOOL
for VAR in var stateicpsr countyicpsr40 C40AGE C40RACE C40IMMIGRANT C40INCOME C40FARM C40HOMEOWN C40WPA C40SCHOOL : drop if VAR == .
                                                    /* Keep only variables used in the CEM */
												
 gen C40NONWHITE = C40RACE != 1 if C40RACE != .     /* We coded white = 1 */
egen countyn     = group(stateicpsr countyicpsr)    /* Numeric county indicator */

**************************************************************************************
****      b.  Loop over every county in the database                              ****
**************************************************************************************
levelsof countyn, local(counties)

foreach county of local counties {                                              /* Because CEM is within county I perform it one county at a time (rows 166-7) and drop the county once I've processed it (row 206-7) */
	preserve
		keep if countyn == `county'

**************************************************************************************
****      c. Create CEM indicators                                                ****
****         i. Treatment: works for WPA                                          ****
**************************************************************************************
		sum C40WPA
                if r(min) == 0 & r(max) == 1 {                                  /* Perform the CEM matching only if there are both treated and controls (at least 1 WPA worker and 1 non-WPA worker) */
                   cem C40NONWHITE (#0) C40IMMIGRANT (#0) C40INCOME (#20) C40SCHOOL(#0) C40AGE (#0)                , treatment(C40WPA)
                   cap drop cem_treat 
                   for VAR in var cem_strata cem_weights cem_matched : ren VAR VAR_wpa
                } /* if C40WPA both 0 & 1 */

**************************************************************************************
****        ii. Treatment: owns a house                                           ****
**************************************************************************************
		sum C40HOMEOWN
                if r(min) == 0 & r(max) == 1 {                                  /* Perform the CEM matching only if there are both treated and controls (at least 1 homeowner and 1 non-homeowner) */
                   cem C40NONWHITE (#0) C40IMMIGRANT (#0) C40INCOME (#20) C40SCHOOL(#0) C40AGE (#0)                , treatment(C40HOMEOWN)
                   cap drop cem_treat 
                   for VAR in var cem_strata cem_weights cem_matched : ren VAR VAR_homeown
                } /* if C40HOMEOWN both 0 & 1 */

**************************************************************************************
****       iii. Treatment: owns a farm                                            ****
**************************************************************************************
		sum C40HOMEOWN if C40FARM == 1
                if r(min) == 0 & r(max) == 1 {                                  /* Perform the CEM matching only if there are both treated and controls (at least 1 farmowner and 1 non-farm owner) */
                   cem C40NONWHITE (#0) C40IMMIGRANT (#0) C40INCOME (#20) C40SCHOOL(#0) C40AGE (#0) if C40FARM == 1, treatment(C40HOMEOWN)
                   cap drop cem_treat 
                   for VAR in var cem_strata cem_weights cem_matched : ren VAR VAR_farmown
                } /* if C40HOMEOWN both 0 & 1 */

**************************************************************************************
****      d. Save CEM indicators for this county and drop it                      ****
**************************************************************************************
		drop stateicpsr countyicpsr40 C40AGE C40RACE C40IMMIGRANT C40INCOME C40FARM C40HOMEOWN C40WPA C40SCHOOL C40NONWHITE countyn
		compress
		save $path/tmp/cem_`county', replace
	restore
	drop if countyn == `county'
} /* foreach county */

**************************************************************************************
****      e. Append CEM indicators for every county                               ****
**************************************************************************************
clear
foreach county of local counties {
	append using $path/tmp/cem_`county'
} /* foreach county */
compress cem_*

**************************************************************************************
****      f. Merge CEM indicator to the matched database                          ****
**************************************************************************************
merge 1:1 C40REG household_identifier individual_identifier using $path/data/C40ASN, nogen keep(mas mat)

drop cem_strata_* cem_weights_*

foreach treat in wpa homeown farmown {
	if "`treat'" == "wpa"      local lab "working for WPA"
	if "`treat'" == "homeown" local lab "owning a home"
	if "`treat'" == "farmown"  local lab "owning a farm"
	lab var cem_matched_`treat'  "Matched by CEM: treatment is `lab'"
} /* foreach treat */

**************************************************************************************
****   6. Match data to county-level information                                  ****
**************************************************************************************
replace countyicpsr = 1210 if stateicpsr == 44 & countyicpsr ==  410 /* Campbell, Georgia already belonged to Fulton in the 1940 shapefile. */
replace countyicpsr = 1210 if stateicpsr == 44 & countyicpsr == 2030 /* Milton, Georgia already belonged to Fulton in the 1940 shapefile. */

replace county      = "Fulton/Campbell/Milton" if countyicpsr == 1210 & stateicpsr == 44

ren      countyicpsr countyicpsr40
merge m:1 stateicpsr countyicpsr using $path/data/patriotism_countyicpsr40, nogen keep(mas mat)
                                                                     /* This merges the individual level data to the county database */

**************************************************************************************
****   7. Create variables of interest                                            ****
**************************************************************************************
 gen farmowner                     = C40FARM == 1 & C40HOMEOWN == 1 if C40FARM != . & C40HOMEOWN != .
 gen iAAA_PF                       = asinh(AAA / A30FARMERS) / 1000
 gen iAAA_PF_farmowner             = iAAA_PF_s * farmowner
 gen iAGRI_PF                      = asinh((AAA + FCA) / A30FARMERS) / 1000
 gen iAGRI_PF_farmowner            = iAGRI_PF_s * farmowner
 gen  SUM3MO_DROUGHT3339           = SUM3MO_DROUGHT1933 + SUM3MO_DROUGHT1934 + SUM3MO_DROUGHT1935 + SUM3MO_DROUGHT1936 + SUM3MO_DROUGHT1937 + SUM3MO_DROUGHT1938 + SUM3MO_DROUGHT1939
 gen iSUM3MO_DROUGHT3339           = asinh(SUM3MO_DROUGHT3339) / 1000
 gen iAGRI_T73                     = asinh(AGRICULTURE_T73) / 1000
 gen iSUM3MO_DROUGHT3339_farmowner = farmowner * iSUM3MO_DROUGHT3339
 gen  SUM3MO_DROUGHT3339_farmowner = farmowner *  SUM3MO_DROUGHT3339
 gen iAGRI_T73_farmowner           = farmowner * iAGRI_T73
 gen C40NONWHITE                   = C40RACE != 1 if C40RACE != .
 gen iC40INCOME                    = asinh(C40INCOME)
 gen icommonlast                   = asinh(commonlast)
 gen icommonfirst                  = asinh(commonfirst)
 gen ilengthlast                   = asinh(lengthlast)
 gen ilengthfirst                  = asinh(lengthfirst)
 gen iHOLC_PH                      = asinh(HOLC / C30HOMEOWN) / 1000
 gen iHOLC_PH_C40HOMEOWN          = C40HOMEOWN * iHOLC_PH

**************************************************************************************
****   8. Label and save                                                          ****
**************************************************************************************
lab var C40WPA       				   "Works for WPA"
lab var C40UNEMP     				   "Unemployed"
lab var farmowner    				   "Farm owner"
lab var C40NONWHITE  				   "Non white"
lab var C40IMMIGRANT 				   "Immigrant"
lab var iC40INCOME   				   "1939 income (i.h.s.)"
lab var C40SCHOOL_E  				   "Elementary school"
lab var C40SCHOOL_H  				   "High school"
lab var C40SCHOOL_C  				   "College"
lab var C40MARRIED   				   "Ever married"
lab var C40FARM      				   "Lives on a farm"
lab var icommonlast  				   "Surname commonness (i.h.s.)"
lab var icommonfirst 				   "Name commonness (i.h.s.)"
lab var ilengthlast  				   "Surname length (i.h.s.)"
lab var ilengthfirst 				   "Name length (i.h.s.)"

lab var iAGRI_PF                      "Agricultural support"
lab var iAGRI_PF_farmowner            "Agricultural support x farm owner"
lab var iSUM3MO_DROUGHT3340_farmowner "Summer droughts: 1933-40 (i.h.s.) x farm owner"
lab var iAGRI_T73_farmowner           "Tenure in Agri committee: 1933-35 (i.h.s.) x farm owner"
lab var iHOLC_PH                      "HOLC loans per home-owner (i.h.s.)"
lab var iHOLC_PH_C40HOMEOWN           "HOLC loans per home-owner  (i.h.s.) x home owner"
lab var C40HOMEOWN                    "Home owner"
																	 
save $path/data/patriotism-nber, replace

**************************************************************************************
****   9. Erase junk created                                                      ****
**************************************************************************************
foreach region in MIDWEST NORTHEAST SOUTH WEST {
	erase "$path/tmp/C40-`region'.dta"
}  /* foreach region */

foreach state in AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY CD IT GE EN MX PO PH IE RU PR CN AT CZ GR HU SE PN NO YU FR RO CU DK PT HO TR ES LT FI BE AB BG JP EG {	
	erase $path/ASN-`state'_r2l.dta
	erase $path/tmp/C40-`state'_r2l.dta
	erase $path/tmp/C40ASN-`state'.dta
	erase $path/tmp/C40ASN-`state'_5yr_band.dta
} /* foreach state */

foreach state in AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY CD IT GE EN MX PO PH IE RU PR CN AT CZ GR HU SE PN NO YU FR RO CU DK PT HO TR ES LT FI BE AB BG JP EG {	
	erase $path/tmp/C40ASN-`state'-ids.dta
} /* foreach state */

erase $path/tmp/C40ASN_5yr_band_ids.dta
erase $path/tmp/C40ASN_5yr_band_hhids.dta

erase $path/tmp/C40ASN-households.dta
erase $path/tmp/C40ASN-heads.dta

foreach region in MIDWEST NORTHEAST SOUTH WEST {
	foreach var_num in 49 53 54 {
		erase $path/tmp/`region'HH-`var_num'.dta
	}  /* forvalues var_num */
	erase $path/tmp/`region'HH.dta
} /* foreach region */

foreach region in MIDWEST NORTHEAST SOUTH WEST {
	forvalues var_num = 59(1)68 {
		erase $path/tmp/`region'-5sample-`var_num'.dta
	} /* forvalues var_num */
	erase $path/tmp/`region'-5sample.dta
} /* foreach region */ 

foreach region in MIDWEST NORTHEAST SOUTH WEST {
	forvalues var_num = 3(1)58 {
		erase $path/tmp/`region'-`var_num'.dta
	} /* forvalues var_num */
	erase $path/tmp/`region'-allvariables.dta
        erase $path/tmp/`region'-headvariables.dta
} /* foreach region */

foreach county of local counties {
	erase $path/tmp/cem_`county'.dta
} /* foreach county */

rmdir "tmp/"

log close
exit
